package com.xhh.check.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhh.check.mapper.CheckMapper;
import com.xhh.check.mapper.IdentityMapper;
import com.xhh.check.service.CheckService;
import com.xhh.model.check.pojos.Identity;
import com.xhh.model.check.pojos.UserIdentity;
import com.xhh.model.check.vos.CheckedVo;
import com.xhh.model.common.dtos.ResponseResult;
import com.xhh.model.common.enums.AppHttpCodeEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class CheckServiceImpl extends ServiceImpl<CheckMapper, UserIdentity> implements CheckService {

    private final IdentityMapper identityMapper;

    /**
     * 校验用户身份
     * @param userId
     * @return
     */
    @Override
    public ResponseResult checkIdentity(Long userId) {
        // 1. 校验参数
        if (userId == null || userId == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2. 查询数据
        // 2.1 根据用户id查询身份id
        UserIdentity userIdentity = lambdaQuery().eq(UserIdentity::getUserId, userId).one();
        if (userIdentity == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 2.1 根据身份id查询身份
        Identity identity = identityMapper.selectById(userIdentity.getIdentityId());
        // 3. 返回结果
        CheckedVo checked = new CheckedVo();
        checked.setChecked(true);
        checked.setIdentityCode(identity.getIdentityCode());
        String json = JSON.toJSONString(checked);
        return ResponseResult.okResult(json);

    }

}
